package com.example.petstoreservice.controller;

import com.example.petstoreservice.entity.Address;
import com.example.petstoreservice.entity.Result;
import com.example.petstoreservice.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody Address address){
        if(addressService.insert(address)){
            return Result.success("添加成功");
        }else{
            return Result.fail("新增失败!");
        }
    }
    @GetMapping("/{userName}")
    public Result<List<Address>> getByUserName(@PathVariable String userName)
    {
        Result<List<Address>> res=new Result<>();
        if(addressService.getByUserName(userName)!=null)
        {
            res.setData(addressService.getByUserName(userName));
            res.setStatusCode("200");
        }else{
            Result.fail("当前用户未创建地址");
        }
        return res;
    }
    @GetMapping("/id/{id}")
    public Result<Address> getById(@PathVariable Long id) {
        Result<Address> res = new Result<>();
        Address address = addressService.getById(id);
        if (address != null) {
            res.setData(address);
            res.setStatusCode("200");
        } else {
            res = Result.fail("地址不存在");
        }
        return res;
    }
    @GetMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id)
    {
        boolean y=addressService.delete(id);
        if(y){
            return Result.success("删除成功!");
        }else{
            return Result.fail("删除失败!");
        }
    }
    @PostMapping("/update")
    public Result<?> update(@RequestBody Address address){
        Long id=address.getAddressId();
        Address currentAddress=addressService.getById(id);
        if(address.getAddressLine()!=null){
            currentAddress.setAddressLine(address.getAddressLine());
        }
        if(address.getCity()!=null){
            currentAddress.setCity(address.getCity());
        }
        if(address.getProvince()!=null){
            currentAddress.setProvince(address.getProvince());
        }
        if(address.getDistrict()!=null){
            currentAddress.setDistrict(address.getDistrict());
        }
        if(address.getContact()!=null){
            currentAddress.setContact(address.getContact());
        }
        if(address.getName()!=null){
            currentAddress.setName(address.getName());
        }
        if (address.getMoRen() == 1) {
            addressService.clearDefaultAddressForUser(currentAddress.getUserName());
            currentAddress.setMoRen(1);
        } else {
            currentAddress.setMoRen(0);
        }
        if(addressService.update(currentAddress)){
            return Result.success("修改成功!");
        }else{
            return Result.fail("修改失败!");
        }
    }
}
